//#define __DEBUG__
// by Xeno
#define THIS_FILE "fn_squadmgmtbuttonclicked.sqf"
#include "x_setup.sqf"

#include "x_uiinc.sqf"

if (GVAR(sqtmgmtblocked)) exitWith {};
private ["_diff", "_grp", "_sidep", "_newgrp", "_count", "_oldgrp", "_disp", "_lbbox", "_lbidx", "_lbname"];
if (typeName _this != typeName 1) exitWith {};
_diff = _this - 5000;
_grp = GVAR(SQMGMT_grps) select _diff;
_oldgrp = group player;
disableSerialization;
// remove unit from group
_disp = __uiGetVar(X_SQUADMANAGEMENT_DIALOG);
_lbbox = 2000 + _diff;
_lbidx = lbCurSel CTRL(_lbbox);
_lbname = if (_lbidx != -1) then {
	CTRL(_lbbox) lbData _lbidx
} else {
	""
};
_ogrlocked = _oldgrp getVariable [QGVAR(locked), false];
if (_ogrlocked && {player == leader _oldgrp}) then {
	_oldgrp setVariable [QGVAR(locked), false];
};
if (group player == _grp && {_lbname != name player} && {_lbidx != -1} && {player == leader _grp}) exitWith {
	_unittouse = objNull;
	{
		if (name _x == _lbname) exitWith {
			_unittouse = _x;
		};
	} forEach (units _grp);
	if (isNull _unittouse) exitWith {};
	// must be AI version
	if (!isPlayer _unittouse) exitWith {
		if (vehicle _unittouse == _unittouse) then {
			deleteVehicle _unittouse;
		} else {
			moveOut _unittouse;
			_unittouse spawn {
				scriptName "spawn_d_fnc_squadmgmtbuttonclicked_vecwait";
				private "_grp";
				_grp = group _this;
				waitUntil {sleep 0.331;vehicle _this == _this};
				deleteVehicle _this;
			};
		};
		if (dialog) then {
			call FUNC(squadmanagementfill);
		};
	};
	_newgrpp = createGroup side (group player);
	[_unittouse] joinSilent _newgrpp;
	GVAR(sqtmgmtblocked) = true;
	[_grp, _unittouse] spawn {
		scriptName "spawn_d_fnc_squadmgmtbuttonclicked_sqmgmtfill";
		waitUntil {!((_this select 1) in (units (_this select 1)))};
		if (dialog) then {
			call FUNC(squadmanagementfill);
		};
		GVAR(sqtmgmtblocked) = false;
	};
};

// Leave = new group
if (group player == _grp) then {
	_sidep = side (group player);
	_newgrp = createGroup _sidep;
	[player] joinSilent _newgrp;
	if (GVAR(with_ai)) then {
		_ai_only = true;
		{
			if (isPlayer _x) exitWith {
				_ai_only = false;
			};
		} forEach (units _grp);
		if (_ai_only) then {
			{
				deleteVehicle _x;
			} forEach (units _grp);
			if (count (units _grp) > 0) then {
				{
					moveOut _x;
					_x spawn {
						scriptName "spawn_d_fnc_squadmgmtbuttonclicked_waitvec2";
						private "_grp";
						_grp = group _this;
						waitUntil {sleep 0.331;vehicle _this == _this};
						deleteVehicle _this;
						deleteGroup _grp;
					};
				} forEach (units _grp)
			};
		};
	};
	if (count (units _grp) == 0) then {
		if (!isNull _grp) then {
			deleteGroup _grp;
		};
	} else {
		[QGVAR(grpswmsg), [leader _grp, name player]] call FUNC(NetCallEventSTO);
	};
	// transfer name of old group to new group ? (setgroup ID) ?
	// edit: not needed, players can't leave groups with just himself in the group
} else {
	_count = 0;
	// TYRGHEN - modified how the group leader is selected for safety
	_leader_grp = leader _grp;
	//[QGVAR(grpjoin), [_grp, player]] call FUNC(NetCallEventSTO);
	{
		if (isPlayer _x) then {
			__INC(_count);
		};
	} forEach (units _grp);
	// if (_count == 1) then {
		// [QGVAR(grpslead), [leader _grp, _grp, player]] call FUNC(NetCallEventSTO);
	// };
	[QGVAR(grpjoin), [_grp, player]] call FUNC(NetCallEventSTO);
	if (_count <= 0) then {
		[QGVAR(grpslead), [leader _grp, _grp, player]] call FUNC(NetCallEventSTO);
	} else {
		[QGVAR(grpslead), [leader _grp, _grp, _leader_grp]] call FUNC(NetCallEventSTO);
	};
	
	if (GVAR(with_ai)) then {
		_ai_only = true;
		{
			if (isPlayer _x) exitWith {
				_ai_only = false;
			};
		} forEach (units _oldgrp);
		if (_ai_only) then {
			{
				deleteVehicle _x;
			} forEach (units _oldgrp);
			if (count (units _oldgrp) > 0) then {
				{
					moveOut _x;
					_x spawn {
						scriptName "spawn_d_fnc_squadmgmtbuttonclicked_waitvec3";
						private "_grp";
						_grp = group _this;
						waitUntil {sleep 0.331;vehicle _this == _this};
						deleteVehicle _this;
						deleteGroup _grp;
					};
				} forEach (units _oldgrp)
			};
		};
	};
	if (count (units _oldgrp) == 0&& {!isNull _oldgrp}) then {
		deleteGroup _oldgrp;
	};
	if (!isNull _oldgrp) then {
		[QGVAR(grpswmsg), [leader _oldgrp, name player]] call FUNC(NetCallEventSTO);
	};
	[QGVAR(grpswmsgn), [leader _grp, name player]] call FUNC(NetCallEventSTO);
};
GVAR(sqtmgmtblocked) = true;
_oldgrp spawn {
	scriptName "spawn_d_fnc_squadmgmtbuttonclicked_oldgrp";
	waitUntil {!(player in (units _this)) || {isNull _this}};
	if (dialog) then {
		call FUNC(squadmanagementfill);
	};
	GVAR(sqtmgmtblocked) = false;
};